
package com.zhu.salesmanagement;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.zhu.location.R;
import com.zhu.salesmanagement.common.Actions;
import com.zhu.salesmanagement.common.GetGpsLocation;
import com.zhu.salesmanagement.network.CustomerShelfData;
import com.zhu.salesmanagement.network.DownloadService;
import com.zhu.salesmanagement.network.ParseData;
import com.zhu.salesmanagement.network.PostMethod;
import com.zhu.salesmanagement.network.XMLRPCMethodCallback;
import com.zhu.salesmanagement.utils.Util;
import com.zhu.salesmanagement.views.JingPinInfoView;
import com.zhu.salesmanagement.views.MyCustomerView;
import com.zhu.salesmanagement.views.NotificationView;
import com.zhu.salesmanagement.views.SyncView;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Parcelable;
import android.provider.MediaStore;
import android.provider.Settings;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SimpleAdapter;

public class MainActivity extends Activity implements View.OnClickListener {

    private GetGpsLocation mGetGpsLocation = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.layout_main);

        setupView();
        initData();
        mGetGpsLocation = new GetGpsLocation(this, Util.mVerifyUserResult.iGPSInterval);
        if(Util.mVerifyUserResult.mDecVersion != null && !Util.mVerifyUserResult.mDecVersion.equals(Util.getVerName(this))){
        	popUpdateDialog();
        }
    }

    private void setupView() {
        findViewById(R.id.main_visit).setOnClickListener(this);
        findViewById(R.id.main_notification).setOnClickListener(this);
        findViewById(R.id.main_exit).setOnClickListener(this);
        findViewById(R.id.main_sync).setOnClickListener(this);
        findViewById(R.id.main_visitpc).setOnClickListener(this);
        findViewById(R.id.main_visitp).setOnClickListener(this);
        //findViewById(R.id.main_storelist).setOnClickListener(this);
    }

    private void initData() {

    }

    private void gotoMyCustomerActivity() {
        Intent tent = new Intent(MainActivity.this, MyCustomerActivity.class);
        MainActivity.this.startActivity(tent);
    }

    private void gotoVisitTaskActivity() {
        Intent tent = new Intent(MainActivity.this, VisitTaskListActivity.class);
        MainActivity.this.startActivity(tent);
    }

    private void gotoRemindListActivity() {
        Intent tent = new Intent(MainActivity.this, RemindListActivity.class);
        MainActivity.this.startActivity(tent);
    }
    
    private void addEvent() {
        Actions.addEvent(MainActivity.this, "Test title", "test content", "wujiaochao",
                System.currentTimeMillis() + 1000 * 60 * 5,
                System.currentTimeMillis() + 1000 * 60 * 65, 3, 1);
    }


    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        if (arg0 != null) {
            switch (arg0.getId()) {
                case R.id.main_visit:
                	gotoVisitTaskActivity();
                    break;
                case R.id.main_exit:
                    popExitDialog();
                    break;
                case R.id.main_notification:
                	gotoRemindListActivity();
                    break;
                case R.id.main_sync:
                	gotoSyncActivity();
                    break;
                case R.id.main_visitpc:
                	gotoCustomersListActivity();
                	break;
                case R.id.main_visitp:
                	gotoProductsListActivity();
                	break;
//                case R.id.main_storelist:
//                	gotoStoreListActivity();
//                	break;
            }
        }
    }

    private void gotoCustomersListActivity(){
    	Intent tent = new Intent(MainActivity.this, CustomersListActivity.class);
        startActivity(tent);
    }

    private void gotoProductsListActivity(){
    	Intent tent = new Intent(MainActivity.this, ProductsListActivity.class);
        startActivity(tent);
    }

    private void gotoSyncActivity(){
        //getAA();
    	Intent tent = new Intent(MainActivity.this, SyncActivity.class);
        startActivity(tent);
    }

    private void gotoStoreListActivity(){
    	Intent tent = new Intent(MainActivity.this, AddedStoreListActivity.class);
        startActivity(tent);
    }

    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        if (keyCode == KeyEvent.KEYCODE_BACK)
        {
        	popExitDialog();
        	return true;
        }
        return false;
    }

    private void popExitDialog() {
        Actions.alertShowDetails(this, getString(R.string.exitclew),
                getString(R.string.exitcontent),
                R.string.queding, R.string.cancel,
                new android.content.DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int arg1) {
                        finish();
                    }
                }, new android.content.DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int arg1) {

                    }
                });
    }

    public void onDestroy() {
        mGetGpsLocation.destroy();
        super.onDestroy();
    }

    private void popUpdateDialog() {
        Actions.alertShowDetails(this, this.getString(R.string.updateclew),
                this.getString(R.string.updatecontent),
                R.string.queding, R.string.cancel,
                new android.content.DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int arg1) {
                        if(Actions.isSDCardMounted()){
                            new DownloadService(MainActivity.this).startDownLoad(Util.mVerifyUserResult.sDownPath, Util.mVerifyUserResult.sFileSize);
                        }else{
                            Actions.toastShort(MainActivity.this, MainActivity.this.getResources().getString(R.string.insertsdcard));
                        }
                    }
                }, new android.content.DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int arg1) {

                    }
                });
    }

    private void getAA(){
        //DownloadService aa = new DownloadService(this);
        //aa.startDownLoad("http://apps.Lianluo.com/virtualbox/testgamemm_ldxd_ipa.apk", 6355093);
//    	PostMethod.requesMyTaskList(Util.mVerifyUserResult.id, new XMLRPCMethodCallback() {  //ERROR
//            public void callFinished(int status, Object result) {
//                ParseData.parseMyTaskList(result, MainActivity.this);
//            }
//        },
//        SalesApplication.mApplicaton.getXMLRPCClient());

//    	PostMethod.requesRemindMsg(Util.mVerifyUserResult.id, new XMLRPCMethodCallback() { //NO DATA
//            public void callFinished(int status, Object result) {
//                ParseData.parseRemindMsg(result, MainActivity.this);
//            }
//        },
//        SalesApplication.mApplicaton.getXMLRPCClient());

//        PostMethod.requesCustomer(Util.mVerifyUserResult.id, true, new XMLRPCMethodCallback() {
//            public void callFinished(int status, Object result) {
//                ParseData.parseCustomer(result, MainActivity.this);
//            }
//        },
//        SalesApplication.mApplicaton.getXMLRPCClient());

    	final String customerId = "10000013";
//    	PostMethod.requesProductCategory(Util.mVerifyUserResult.id, customerId, true, new XMLRPCMethodCallback() {
//            public void callFinished(int status, Object result) {
//                ParseData.parseProductCategory(result, customerId, MainActivity.this);
//            }
//        },
//        SalesApplication.mApplicaton.getXMLRPCClient());

//    	final String productCategoryId = "XC";  //CT, YP
//    	final String productCategoryId1 = "CT";  //CT, YP
//    	final String productCategoryId2 = "YP";  //CT, YP
//    	List<Object> list = new ArrayList<Object>();
//    	list.add(productCategoryId);
//    	list.add(productCategoryId1);
//    	list.add(productCategoryId2);
//    	PostMethod.requesProducts(Util.mVerifyUserResult.id, list, true, new XMLRPCMethodCallback() {  //XC NO DATA
//    		public void callFinished(int status, Object result) {
//    			ParseData.parseProducts(result, MainActivity.this);
//    		}
//    	},
//    	SalesApplication.mApplicaton.getXMLRPCClient());

//    	PostMethod.requesCustomerShelfs(Util.mVerifyUserResult.id, customerId, list, true, new XMLRPCMethodCallback() {  //API ERROR CustomerShelf C lowercase
//
//    		public void callFinished(int status, Object result) {
//    			ParseData.parseCustomerShelfs(result, MainActivity.this);
//    		}
//    	},
//    	SalesApplication.mApplicaton.getXMLRPCClient());

//    	CustomerShelfData data = new CustomerShelfData(this, Util.mVerifyUserResult.id);
//    	PostMethod.postCustomerShelf(data.getData(), new XMLRPCMethodCallback() {  //API ERROR CustomerShelf C lowercase
//    		public void callFinished(int status, Object result) {
//    			Log.e("zhu", "postCustomerShelf=" + result);
//    		}
//    	},
//    	SalesApplication.mApplicaton.getXMLRPCClient());
    }
}
